package training;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class J25_6_7_Training {
    //自行车
    static class edge{
        public int u;
        public int v;
        public Long weight;

        public edge(int u, int v, long weight) {
            this.u = u;
            this.v = v;
            this.weight = weight;
        }
    }

    static int n;
    static int m;
    static int[] parent;
    static List<edge> Edge = new ArrayList<>();
    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        parent = new int[n+1];
        for (int i = 1; i <= m; i++) {
            int u = scanner.nextInt();
            int v = scanner.nextInt();
            Long w = scanner.nextLong();
            Edge.add(new edge(u,v,w));
        }
        Edge.sort((a,b)->{
            return (int)(a.weight - b.weight);
        });
        for (int i = 1; i <= n; i++) {
            parent[i] = i;
        }

        kruskal();
    }

    public static int find(int x){
        if(parent[x] != x){
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }

    public static void union(int x,int y){
        int rootx = find(x);
        int rooty = find(y);
        parent[rootx] = rooty;
    }

    public static void kruskal(){
        long ret = 0;
        int MAXm = 0;
        for (edge x : Edge){
            if(find(x.u) == find(x.v)){
                continue;
            }
            MAXm++;
            ret += x.weight;
            union(x.u,x.v);
            if(MAXm == n-1){
                break;
            }
        }

        if(MAXm<n-1){
            System.out.println(-1);
        }else{
            System.out.println(ret);
        }
    }

    //游戏
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();
        long[] nums = new long[n+1];
        for (int i = 1; i <= n; i++) {
            nums[i] = scanner.nextLong();
        }


    }
}
